package UPPM;

import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import UPPM.Utils;

public class TestQuickSearchOptions 
{
	
	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");	
		}	
	

	@Test
	public void testQuickSearchTabInitialState() throws Exception {

		WebElement search_btn = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[2]/div[6]/div[2]/div/div/form/input[2]"));
		Assert.assertEquals(true,search_btn.isDisplayed());
		
				
		WebElement quick_search_field = driver.findElement(By.id("quick_search_field"));
		Assert.assertEquals(true, quick_search_field.isDisplayed());
		

		WebElement searchCriteria = driver.findElement(By.id("quick_search_criteria_button"));
		Assert.assertEquals(true, searchCriteria.isDisplayed());

		WebElement searchHistory = driver.findElement(By.id("search_history"));
		Assert.assertEquals(true, searchHistory.isDisplayed());

		WebElement quickSearchBtn = driver.findElement(By
				.id("quick_search_btn"));
		Assert.assertEquals(true, quickSearchBtn.isDisplayed());

	}

	//check search attributes
	@Test
	public void testQuickSearchAttributesSection() throws Exception {
		WebElement quick_search =  driver.findElement(By.className("ac_input"));
		driver.findElement(By.className("ac_input")).clear();
		quick_search.click();
		Thread.sleep(1000);
        
		Utils.waitForElement(driver, By.id("qk_search_opts"));
		WebElement search_opts = driver.findElement(By.id("qk_search_opts"));
//		search_opts.click();
		
		Assert.assertEquals(true, search_opts.isDisplayed());
		WebElement Btn_select_all = driver.findElement(By.id("select_all"));
		Assert.assertEquals(true, Btn_select_all.isDisplayed());
		WebElement btn_select_none = driver.findElement(By.id("select_none"));
		Assert.assertEquals(true, btn_select_none.isDisplayed());
		WebElement btn_close = driver.findElement(By.id("close"));
		Assert.assertEquals(true, btn_close.isDisplayed());
		}
	
	

	@Test
	public void testQuickSearchNoResultsFunctionalityUserNotLogedIn() throws Exception {

		WebElement quickSearchField = driver.findElement(By.id("quick_search_field"));
		quickSearchField.sendKeys("testing no results");
		WebElement quickSearchBtn = driver.findElement(By.id("quick_search_btn"));
		quickSearchBtn.click();

		// check-ups in the search results page
		WebElement playlist_exp = driver.findElement(By.xpath("//*[@id='outline']/div/div[4]/div/div[1]/div/div[2]/div/div[1]/div/div/h2"));
		Assert.assertEquals("Playlists Explorer", playlist_exp.getText());
		//check that there is a message displayed
		WebElement no_results_mesage = driver.findElement(By.id("no_results_message"));
		Assert.assertEquals(true, no_results_mesage.isDisplayed());
			
	}


	
	
	@After
	public void tearDown() throws Exception {
		System.out.println("******************************");
		driver.quit();
	}
}